
const http = require('http')

const Router = require('./router')

function Application(){
    this.router = new Router()   //创建应用同时创建一个路由系统
}
Application.prototype.get = function(pathname,...handlers){
    // this.router.push({
        // method:"get",
        // pathname,
        // handler
    // })
    this.router.get(pathname,handlers)
}
Application.prototype.listen = function(){
    const server = http.createServer((req,res)=>{
        // 当路由系统处理不了，返回找不到
        function done(){
            res.end(`cannot  ${req.method} ${req.url}`)
        }
        this.router.handle(req,res,done)
        // let {pathname} = url.parse(req.url)
        // let method = req.method.toLowerCase()
        // for (let i = 1; i < this.router.length; i++) {
        //     let {method:routeMethod,pathname:routePath,handler} = this.router[i]
        //     if(pathname === routePath && method === routeMethod) {
        //        return handler(req,res)
        //     }
        // }
        // this.router[0].handler(req,res)

    })
    server.listen(...arguments)
}
module.exports = Application



